class Solution {
public:
    int numSubarraysWithSum(vector<int>& nums, int goal)
    {
        int n = nums.size();
        int ans = 0;
        //vector<int> f(n,0);
        unordered_map<int, int> hash;
        int f = 0;
        for (int i = 0; i < n; i++)
        {
            hash[f]++;
            f += nums[i];
            ans += hash[f - goal];
        }
        //ans += hash[goal];
        return ans;
    }
};